#include "../config.h"
#include "../feature.h"
#include<stdio.h>
#include <stdlib.h>

void test_config_file_get() {
    FILE *f = config_file_get();
    char *ptr = NULL;
    if (f) {
        do {
            free(ptr);
            ptr = file_line_get(f);
            printf("Config:  '%s'\n", ptr);
        } while (ptr);
        fclose(f);
    }
}

void feature_get_test() {
    const char *tcs [] = {"   PATH=\"/etc/mokoko.conf;/usr/etc/mokoko.conf\"",
                          "#PATH=\"/etc/mokoko.conf;/usr/etc/mokoko.conf\"",
                          "PATH=\"/etc/mokoko.conf;/usr/etc/mokoko.conf\"",
                          "PATH = \"/etc/mokoko.conf;/usr/etc/mokoko.conf\"",
                          "PATH \"/etc/mokoko.conf;/usr/etc/mokoko.conf\"",
                          NULL
                         };
    int i;
    char *feature;
    for (i = 0;tcs[i];i++) {
        printf("Test Case is '%s'\n", tcs[i]);
        feature = feature_get(tcs[i]);
        printf("feature = '%s'\n", feature);
        free(feature);
    }
}

void token_get_test() {
    const char *tcs [] = {"   PATH=\"/etc/mokoko.conf;/usr/etc/mokoko.conf\"",
                          "#PATH=\"/etc/mokoko.conf;/usr/etc/mokoko.conf\"",
                          "PATH=\"/etc/mokoko.conf;/usr/etc/mokoko.conf\"",
                          "PATH = \"/etc/mokoko.conf;/usr/etc/mokoko.conf\"",
                          "PATH \"/etc/mokoko.conf;/usr/etc/mokoko.conf\"",
                          "PATH \"/etc/mokoko.conf\"",
                          "\"\"",
                          "\";;;;\"",
                          "\"",
                          "PATH=lalal;ldsfds;kjsdhfjksf;dsd;;",
                          "\"\"\"\";;;\"\";;;",
                          NULL
                         };
    int i, j;
    char *token;
    for (i = 0;tcs[i];i++) {
        printf("token_get_test: tcs[%d] '%s'\n", i, tcs[i]);
        for (j = 0;;j++) {
            token = token_get((const char *)tcs[i], j);
            if (token == NULL) {
                printf("tcs[%d] token no. %d is NULL\n", i, j);
                break;
            }
            printf("token[%d]=='%s'\n", j, token);
            free(token);
        }
    }
}

void config_read_test() {
    Features *FS;
    Feature *F;
    Feature_Value *FV;
    config_read();
    printf("mokoko_features_count == %d\n", mokoko_features_count());
    FS = mokoko_features_singlton_get ();
    list_for_each_entry(F, &FS->feature_head, node) {
        printf("Feature '%s' has %d values\n", F->name, mokoko_feature_value_count(NULL, F->name));
        list_for_each_entry(FV, &F->value_head, node) {
            printf("\t%s\n", FV->value);
        }
        printf("\n");
    }
}

int main (void) {
    test_config_file_get();
    feature_get_test();
    token_get_test();
    config_read_test();
    mokoko_features_cleanup(NULL);
}
